﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;

namespace CommunitySystem.PageView
{
    public partial class BianZhiWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.BianZhiModel model = new EmpbasicService.BianZhiModel();
        public int ID;
        public int OrgID;
        public bool IsNew = false;
        public BianZhiWindow(int orgID, int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            OrgID = orgID;
            IsNew = isnew;
            Client.GetBianZhiModelShiYouCompleted += new EventHandler<EmpbasicService.GetBianZhiModelShiYouCompletedEventArgs>(Client_GetBianZhiModelShiYouCompleted);
            Client.GetBianZhiModelShiYouAsync(OrgID);
            if (IsNew)
            {
                this.Title = "新增编制信息";
                model = new EmpbasicService.BianZhiModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑编制信息";
                Client.GetBianZhiModelCompleted += new EventHandler<EmpbasicService.GetBianZhiModelCompletedEventArgs>(Client_GetBianZhiModelCompleted);
                Client.GetBianZhiModelAsync("", ID);
            }


        }

        void Client_GetBianZhiModelShiYouCompleted(object sender, EmpbasicService.GetBianZhiModelShiYouCompletedEventArgs e)//实有
        {
            //zongbianzhi1.Text = e.Result[0].ZongBianZhiShiYou;
            zhengkelingdao1.Text = !string.IsNullOrEmpty(e.Result[0].ZhengKeLingDaoShiYou)?e.Result[0].ZhengKeLingDaoShiYou:"0";
            zhengkefeilingdao1.Text = !string.IsNullOrEmpty(e.Result[0].ZhengKeFeiLingDaoShiYou) ? e.Result[0].ZhengKeFeiLingDaoShiYou : "0";
            fukelingdao1.Text = !string.IsNullOrEmpty(e.Result[0].FuKeLingDaoShiYou) ? e.Result[0].FuKeLingDaoShiYou : "0";
            fukefeilingdao1.Text = !string.IsNullOrEmpty(e.Result[0].FuKeFeiLingDaoShiYou) ? e.Result[0].FuKeFeiLingDaoShiYou : "0";
            keyuan1.Text = !string.IsNullOrEmpty(e.Result[0].KeYuanShiYou) ? e.Result[0].KeYuanShiYou : "0";
            weidingzhi1.Text = !string.IsNullOrEmpty(e.Result[0].WeiDingZhi) ? e.Result[0].WeiDingZhi : "0";
            keji.Text = (Convert.ToInt32(zhengkelingdao1.Text) + Convert.ToInt32(zhengkefeilingdao1.Text)
                         + Convert.ToInt32(fukelingdao1.Text) + Convert.ToInt32(fukefeilingdao1.Text)
                         + Convert.ToInt32(keyuan1.Text) + Convert.ToInt32(weidingzhi1.Text)).ToString();
            result();
        }

        void Client_GetBianZhiModelCompleted(object sender, EmpbasicService.GetBianZhiModelCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            this.DataContext = model;
            result();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsNew == true)
            {
                model.OrgID = OrgID;
                Client.AddBianZhiModelCompleted += new EventHandler<EmpbasicService.AddBianZhiModelCompletedEventArgs>(Client_AddBianZhiModelCompleted);
                Client.AddBianZhiModelAsync(model);
            }
            else
            {
                Client.UpdateBianZhiModelCompleted += new EventHandler<EmpbasicService.UpdateBianZhiModelCompletedEventArgs>(Client_UpdateBianZhiModelCompleted);
                Client.UpdateBianZhiModelAsync(model);
            }
        }

        public void result()
        {
            if (!string.IsNullOrEmpty(model.ZongBianZhi) && !string.IsNullOrEmpty(model.ChuJiRenShu))
            {
                if((Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(model.ChuJiRenShu) - Convert.ToInt32(keji.Text)) < 0)
                {
                    kongbian.Text =
                        (Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(model.ChuJiRenShu) -
                         Convert.ToInt32(keji.Text)).ToString();
                    kongbian.Foreground = new SolidColorBrush(Colors.Red);
                    kongbian.FontWeight = FontWeights.Bold;
                }
                else
                {
                    kongbian.Text =
                        (Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(model.ChuJiRenShu) -
                         Convert.ToInt32(keji.Text)).ToString();
                    kongbian.Foreground = new SolidColorBrush(Colors.Black);
                    kongbian.FontWeight = FontWeights.Normal;
                }

            }
            if (!string.IsNullOrEmpty(model.ZhengKeLingDao))
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(zhengkelingdao1.Text) ? "0" : zhengkelingdao1.Text) - Convert.ToInt32(model.ZhengKeLingDao)) <= 0)
                {
                    zhengkelingdao2.Text = "无";
                    zhengkelingdao2.FontWeight = FontWeights.Normal;
                    zhengkelingdao2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    zhengkelingdao2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(zhengkelingdao1.Text) ? "0" : zhengkelingdao1.Text) - Convert.ToInt32(model.ZhengKeLingDao)).ToString();
                    zhengkelingdao2.Foreground = new SolidColorBrush(Colors.Red);
                    zhengkelingdao2.FontWeight = FontWeights.Bold;
                }
            }
            if (!string.IsNullOrEmpty(model.ZhengKeFeiLingDao))
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(zhengkefeilingdao1.Text) ? "0" : zhengkefeilingdao1.Text) - Convert.ToInt32(model.ZhengKeFeiLingDao)) <= 0)
                {
                    zhengkefeilingdao2.Text = "无";
                    zhengkefeilingdao2.FontWeight = FontWeights.Normal;
                    zhengkefeilingdao2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    zhengkefeilingdao2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(zhengkefeilingdao1.Text) ? "0" : zhengkefeilingdao1.Text) - Convert.ToInt32(model.ZhengKeFeiLingDao)).ToString();
                    zhengkefeilingdao2.Foreground = new SolidColorBrush(Colors.Red);
                    zhengkefeilingdao2.FontWeight = FontWeights.Bold;
                }
            }
            if (!string.IsNullOrEmpty(model.FuKeLingDao))
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(fukelingdao1.Text) ? "0" : fukelingdao1.Text) - Convert.ToInt32(model.FuKeLingDao)) <= 0)
                {
                    fukelingdao2.Text = "无";
                    fukelingdao2.FontWeight = FontWeights.Normal;
                    fukelingdao2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    fukelingdao2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(fukelingdao1.Text) ? "0" : fukelingdao1.Text) - Convert.ToInt32(model.FuKeLingDao)).ToString();
                    fukelingdao2.Foreground = new SolidColorBrush(Colors.Red);
                    fukelingdao2.FontWeight = FontWeights.Bold;
                }
            }
            if (!string.IsNullOrEmpty(model.FuKeFeiLingDao))
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(fukefeilingdao1.Text) ? "0" : fukefeilingdao1.Text) - Convert.ToInt32(model.FuKeFeiLingDao)) <= 0)
                {
                    fukefeilingdao2.Text = "无";
                    fukefeilingdao2.FontWeight = FontWeights.Normal;
                    fukefeilingdao2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    fukefeilingdao2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(fukefeilingdao1.Text) ? "0" : fukefeilingdao1.Text) - Convert.ToInt32(model.FuKeFeiLingDao)).ToString();
                    fukefeilingdao2.Foreground = new SolidColorBrush(Colors.Red);
                    fukefeilingdao2.FontWeight = FontWeights.Bold;
                }
            }
            if (!string.IsNullOrEmpty(model.KeYuan))
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(keyuan1.Text) ? "0" : keyuan1.Text) - Convert.ToInt32(model.KeYuan)) <= 0)
                {
                    keyuan2.Text = "无";
                    keyuan2.FontWeight = FontWeights.Normal;
                    keyuan2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    keyuan2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(keyuan1.Text) ? "0" : keyuan1.Text) - Convert.ToInt32(model.KeYuan)).ToString();
                    keyuan2.Foreground = new SolidColorBrush(Colors.Red);
                    keyuan2.FontWeight = FontWeights.Bold;
                }
            }
        }
        public void result1(string text,int sec)
        {
            if (sec == 1)
            {
                if (!string.IsNullOrEmpty(model.ZongBianZhi) && !string.IsNullOrEmpty(text))
                {
                    if ((Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(text) - Convert.ToInt32(!string.IsNullOrEmpty(keji.Text)?keji.Text:"0")) < 0)
                    {
                        kongbian.Text =
                            (Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(text) -
                             Convert.ToInt32(keji.Text)).ToString();
                        kongbian.Foreground = new SolidColorBrush(Colors.Red);
                        kongbian.FontWeight = FontWeights.Bold;
                    }
                    else
                    {
                        kongbian.Text =
                            (Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(text) -
                             Convert.ToInt32(keji.Text)).ToString();
                        kongbian.Foreground = new SolidColorBrush(Colors.Black);
                        kongbian.FontWeight = FontWeights.Normal;
                    }

                }
            }
            if (sec == 2)
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(zhengkelingdao1.Text) ? "0" : zhengkelingdao1.Text) - Convert.ToInt32(text)) <= 0)
                {
                    zhengkelingdao2.Text = "无";
                    zhengkelingdao2.FontWeight = FontWeights.Normal;
                    zhengkelingdao2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    zhengkelingdao2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(zhengkelingdao1.Text) ? "0" : zhengkelingdao1.Text) - Convert.ToInt32(text)).ToString();
                    zhengkelingdao2.Foreground = new SolidColorBrush(Colors.Red);
                    zhengkelingdao2.FontWeight = FontWeights.Bold;
                }
            }
            if (sec == 3)
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(zhengkefeilingdao1.Text) ? "0" : zhengkefeilingdao1.Text) - Convert.ToInt32(text)) <= 0)
                {
                    zhengkefeilingdao2.Text = "无";
                    zhengkefeilingdao2.FontWeight = FontWeights.Normal;
                    zhengkefeilingdao2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    zhengkefeilingdao2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(zhengkefeilingdao1.Text) ? "0" : zhengkefeilingdao1.Text) - Convert.ToInt32(text)).ToString();
                    zhengkefeilingdao2.Foreground = new SolidColorBrush(Colors.Red);
                    zhengkefeilingdao2.FontWeight = FontWeights.Bold;
                }
            }
            if (sec == 4)
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(fukelingdao1.Text) ? "0" : fukelingdao1.Text) - Convert.ToInt32(text)) <= 0)
                {
                    fukelingdao2.Text = "无";
                    fukelingdao2.FontWeight = FontWeights.Normal;
                    fukelingdao2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    fukelingdao2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(fukelingdao1.Text) ? "0" : fukelingdao1.Text) - Convert.ToInt32(text)).ToString();
                    fukelingdao2.Foreground = new SolidColorBrush(Colors.Red);
                    fukelingdao2.FontWeight = FontWeights.Bold;
                }
            }
            if (sec == 5)
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(fukefeilingdao1.Text) ? "0" : fukefeilingdao1.Text) - Convert.ToInt32(text)) <= 0)
                {
                    fukefeilingdao2.Text = "无";
                    fukefeilingdao2.FontWeight = FontWeights.Normal;
                    fukefeilingdao2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    fukefeilingdao2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(fukefeilingdao1.Text) ? "0" : fukefeilingdao1.Text) - Convert.ToInt32(text)).ToString();
                    fukefeilingdao2.Foreground = new SolidColorBrush(Colors.Red);
                    fukefeilingdao2.FontWeight = FontWeights.Bold;
                }
            }
            if (sec == 6)
            {
                if ((Convert.ToInt32(string.IsNullOrEmpty(keyuan1.Text) ? "0" : keyuan1.Text) - Convert.ToInt32(text)) <= 0)
                {
                    keyuan2.Text = "无";
                    keyuan2.FontWeight = FontWeights.Normal;
                    keyuan2.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    keyuan2.Text =
                        (Convert.ToInt32(string.IsNullOrEmpty(keyuan1.Text) ? "0" : keyuan1.Text) - Convert.ToInt32(text)).ToString();
                    keyuan2.Foreground = new SolidColorBrush(Colors.Red);
                    keyuan2.FontWeight = FontWeights.Bold;
                }
            }
            if (sec == 7)
            {
                if (!string.IsNullOrEmpty(model.ZongBianZhi) && !string.IsNullOrEmpty(text))
                {
                    if ((Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(text) - Convert.ToInt32(keji.Text)) < 0)
                    {
                        kongbian.Text =
                            (Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(text) -
                             Convert.ToInt32(keji.Text)).ToString();
                        kongbian.Foreground = new SolidColorBrush(Colors.Red);
                        kongbian.FontWeight = FontWeights.Bold;
                    }
                    else
                    {
                        kongbian.Text =
                            (Convert.ToInt32(model.ZongBianZhi) - Convert.ToInt32(text) -
                             Convert.ToInt32(keji.Text)).ToString();
                        kongbian.Foreground = new SolidColorBrush(Colors.Black);
                        kongbian.FontWeight = FontWeights.Normal;
                    }

                }
            }
        }
        void Client_UpdateBianZhiModelCompleted(object sender, EmpbasicService.UpdateBianZhiModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                result();
            }
        }

        void Client_AddBianZhiModelCompleted(object sender, EmpbasicService.AddBianZhiModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                result();
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern);   // 匹配正则表达式
            if (!m.Success)   // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
            int sec = 0;
            if (((TextBox)sender).Name == "zongbianzhi")
                sec = 1;
            if (((TextBox)sender).Name == "zhengkelingdao")
                sec = 2;
            if (((TextBox)sender).Name == "zhengkefeilingdao")
                sec = 3;
            if (((TextBox)sender).Name == "fukelingdao")
                sec = 4;
            if (((TextBox)sender).Name == "fukefeilingdao")
                sec = 5;
            if (((TextBox)sender).Name == "keyuan")
                sec = 6;
            if (((TextBox)sender).Name == "chuji")
                sec = 7;
            result1(((TextBox)sender).Text,sec);
        }
    }
}
